/******************************************************************************
 * Copyright (C) 2011 ShenZhen ComTop Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳康拓普开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.code.common.excel.officeexport.excel.process;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * POI根据模版导出
 * 
 * @author 李鹏
 * @since 1.0
 * @version 2012-11-8 李鹏
 */
public class ExcelExportAdapter {

	/** 配置信息 */
	private String fileName;

	/** 配置信息 */
	private String templateFile;

	/** 用户自定义导出标题 */
	public static final String EXCEL_EXPORT_MAP_CHOOSE_TITLE = "excel.export.map.choose.title";

	/** 用户自定义导出所选标题key */
	public static final String EXCEL_EXPORT_LIST_CHOOSE_TITLE = "excel.export.list.choose.title";

	public static final String EXCEL_EXPORT_TITLE = "excel.export.title";

	public ExcelExportAdapter(String fileName, String templateFile) {
		this.fileName = fileName;
		this.templateFile = templateFile;

	}

	// /**
	// * 导出方法针对DWR
	// *
	// * @param request request
	// * @param response response
	// * @throws IOException 如果在输入或者输出过程当中出现错误就抛出异常IOException.
	// */
	// @SuppressWarnings("unchecked")
	// public FileTransfer creatExcelForDWR(List lstRecord) throws IOException {
	// // 调用解析配置文件方法读取配置信息
	// // ServletOutputStream objOut = null;
	// // objOut = this.setResponse(this.fileName, response);
	// // 将获取的配置文件名作为参数，调用加载数据方法
	// ExcelExportByExcel objExportDataLoader = new ExcelExportByExcel();
	// List<Object> lstData = objExportDataLoader.loadExcel(this.templateFile, lstRecord);
	// ExportPrinter objExportPrinter = new ExportPrinter(lstData, this.fileName,
	// this.templateFile);
	// objExportPrinter.writeObject(objExportDataLoader.getWorkbook());
	// return new FileTransfer(new String(fileName.getBytes("GBK"), "iso8859-1"), "application/xls",
	// objExportPrinter.write());
	// }

	/**
	 * 导出方法 Request
	 * 
	 * @param request request
	 * @param response response
	 * @throws IOException 如果在输入或者输出过程当中出现错误就抛出异常IOException.
	 */
	@SuppressWarnings("unchecked")
	public void creatExcelForRequest(List lstRecord, HttpServletResponse response,
			HttpServletRequest request) throws IOException {
		// 将获取的配置文件名作为参数，调用加载数据方法
		response.setContentType("application/x-download");
		response.addHeader("Content-Disposition", "attachment;filename="
				+ getFileName(fileName, request));
		ExcelExportByExcel objExportDataLoader = new ExcelExportByExcel();
		List<Object> lstData = objExportDataLoader.loadExcel(this.templateFile, lstRecord);
		ExportPrinter objExportPrinter = new ExportPrinter(lstData, this.fileName,
				this.templateFile);
		objExportPrinter.writeObject(objExportDataLoader.getWorkbook(), response);
	}

	/**
	 * 导出方法 Request
	 * 
	 * @param request request
	 * @param response response
	 * @throws IOException 如果在输入或者输出过程当中出现错误就抛出异常IOException.
	 */
	@SuppressWarnings("unchecked")
	public void creatExcelForRequest(List lstRecord, OutputStream out) throws IOException {
		// 将获取的配置文件名作为参数，调用加载数据方法
		ExcelExportByExcel objExportDataLoader = new ExcelExportByExcel();
		List<Object> lstData = objExportDataLoader.loadExcel(this.templateFile, lstRecord);
		ExportPrinter objExportPrinter = new ExportPrinter(lstData, this.fileName,
				this.templateFile);
		objExportPrinter.writeObject(objExportDataLoader.getWorkbook(), out);
	}

	// /**
	// * 导出方法
	// *
	// * @param request request
	// * @param response response
	// * @throws Exception
	// */
	// @SuppressWarnings("unchecked")
	// public FileTransfer creatDynaExcel(List lstRecord, Map objMap) throws Exception {
	// ExportPrinter objExportPrinter = new ExportPrinter(this.fileName);
	// ExcelExportByExcel objExportDataLoader = new ExcelExportByExcel();
	// objExportPrinter.writeDynaObject(objExportDataLoader.createDynaExcel(lstRecord, objMap));
	// return new FileTransfer(new String(fileName.getBytes("GBK"), "iso8859-1"), "application/xls",
	// objExportPrinter
	// .write());
	// }

	/**
	 * 解决乱码问题
	 */
	public String getFileName(String fileName, HttpServletRequest request)
			throws UnsupportedEncodingException {
		/** 解决FF下载时文件名乱码问题 */
		String userAgent = request.getHeader("User-Agent");
		if (userAgent != null) {
			userAgent = userAgent.toLowerCase();
			if (userAgent.indexOf("firefox") != -1) {
				fileName = new String(fileName.getBytes(), "ISO-8859-1");
			} else {
				fileName = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
			}
		} else {
			fileName = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
		}
		return fileName;
	}
}
